if orders and not orders.GetTakeEvenPrice then


function orders:CalcTakeEvenPrice(lm, sl, commission)
	local price
	
	if lm < sl then		-- short
		price = lm * (1 - (commission or 0)) + lm - sl
	else				-- long
		price = lm * (1 + (commission or 0)) + lm - sl
	end
	
	return price
end


function orders:CalcStopEvenPrice(lm, tev, qlm, qtev, commission)
	local price
	
	if lm ~= tev then
		if lm > tev then	-- short
			price = lm * (1 - (commission or 0))
		else				-- long
			price = lm * (1 + (commission or 0))
		end
		
		if qlm > qtev then
			price = price + (lm - tev) * qtev / (qlm - qtev)
		end
	end
	
	return price
end


function orders:SetEven(sec_code, is_long, commission)
	if type(sec_code) == "string" then
		local price
		
		price, is_long = self:GetLastTradePrice(sec_code, is_long)
			
		if price then
			if is_long then
				price = tr.price * (1 + (commission or 0))
			else
				price = tr.price * (1 - (commission or 0))
			end
			
			self:SetStopPrice(sec_code, price)
		end
	end
end


function orders:ShiftEven(sec_code, sl, tev, pos, qtev, stop_limit)
	if sl and tev and pos > qtev and qtev > 0 then
		local instrument = lib:GetInstrument(sec_code)
		
		if instrument then
			local is_long = tev > sl
			
			message("ShiftEven tev " .. tostring(tev) .. " is_long " .. tostring(is_long) .. " pos " .. tostring(pos) .. " qtev " .. tostring(qtev))
						
			local lm = orders:GetLastTradePrice(instrument.code, is_long)
			
			if lm and (is_long and tev > lm or not is_long and tev < lm) then
				message("ShiftEven lm " .. tostring(lm))
				
				if not stop_limit then
					stop_limit = lm
				end
						
				local risk, max_lots, commission = lib:CalcRisk(instrument, lm, sl, pos)
				
				if commission then
					message("ShiftEven commission " .. tostring(commission))
						
					local price = orders:CalcStopEvenPrice(lm, tev, pos, qtev, commission)
					message("ShiftEven new stop " .. tostring(price))
					
					if price and (is_long and price > stop_limit or not is_long and price < stop_limit) then
						price = stop_limit
					end
						
					if price and (is_long and price > sl or not is_long and price < sl) then	-- no increase stop
						
						message("ShiftEven stop " .. tostring(sl) .. " -> " .. tostring(price))
						
						orders:SetStopPrice(sec_code, price)
					else
						message("ShiftEven no increase stop " .. tostring(sl) .. " -> " .. tostring(price))
					end
				end
			end
		end
	end
end


end -- of file
